//
//  openVipView.swift
//  demo01
//
//  Created by babybus on 2025/7/1.
//
import Foundation
import UIKit
import SnapKit

class OpenVipView:UIView{
    //全局变量设置
    private var buttonForVip:MyButton!
    private var lableForVip:UILabel!
    
    //初始化方法
    override init(frame: CGRect) {
        super.init(frame: frame)
        attributeInit()
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //属性设置
    func attributeInit(){
        backgroundColor = .systemPink
        layer.cornerRadius = 15
    }
    //布局设置
    func setupUI(){
        //按钮初始化
        buttonForVip = MyButton()
//        buttonForVip.configure(title: "开通会员", titleFor: .normal, titleColor: .white, titleColorFor: .normal, backgroundColor: .black)
        buttonForVip.setImage(UIImage(named: "./openVip.png"), for: .normal)
        buttonForVip.layer.cornerRadius = 11
        buttonForVip.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        
        //标签初始化
        lableForVip = UILabel()
        lableForVip.text = "限时特惠，全年最低价"
        lableForVip.textColor = .white
        
        //将按钮和标签放到view中
        self.addSubview(lableForVip)
        self.addSubview(buttonForVip)
        
        
        //组件布局
        lableForVip.snp.makeConstraints({make in
            make.width.greaterThanOrEqualTo(80)
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().inset(10)
            make.height.equalTo(30)
        })
        buttonForVip.snp.makeConstraints({make in
            make.width.greaterThanOrEqualTo(67)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().inset(10)
            make.height.equalTo(50)
        })
        //渐变色
        let gradientColor = UIColor { traitCollection in
            let colors = traitCollection.userInterfaceStyle == .dark ?
                [UIColor.black, UIColor.purple] :
                [UIColor.white, UIColor.systemPink]
            
            let gradient = CAGradientLayer()
            gradient.colors = colors.map { $0.cgColor }
            gradient.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
            
            UIGraphicsBeginImageContext(gradient.bounds.size)
            gradient.render(in: UIGraphicsGetCurrentContext()!)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            
            return UIColor(patternImage: image!)
        }
        self.backgroundColor = gradientColor
    }
}
